package com.xintonghua.bussiness.ui.user.store;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.SureOrderAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CartBean;
import com.xintonghua.bussiness.bean.CartParams;
import com.xintonghua.bussiness.event.CloseShopEventBus;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/4/1.
 * 包名：com.xintonghua.bussiness.ui.user.store
 */

public class SureOrderActivity extends BaseActivity {
    @BindView(R.id.lv_cart)
    ListView lvCart;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_sure_order)
    TextView tvSureOrder;
    private List<CartBean> list;
    private double money = 0.00;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sure_order);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("确认订单");
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        list = getIntent().getParcelableArrayListExtra(BuildConfig.FLAVOR);
        SureOrderAdapter adapter = new SureOrderAdapter(list, this);
        lvCart.setAdapter(adapter);
        for (CartBean bean : list) {
            money = money + bean.getMoney() * bean.getCount();
        }
        tvAllMoney.setText(MyUtils.getMoney(this, money));

    }


    @OnClick(R.id.tv_sure_order)
    public void onViewClicked() {
        List<CartParams> orderList = new ArrayList<>();
        for (CartBean bean : list) {
            orderList.add(new CartParams(bean.getId(), bean.getCount()));
        }
        httpCent.saveOrder(JSONObject.toJSON(orderList).toString(), money, this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                mToast("订单已生成");
                EventBus.getDefault().register(new CloseShopEventBus());
                finish();
                break;
        }
    }
}
